var app = getApp();
Page({
    data:{
        goodsInfo:[],
        cartId:0,
        userId:0,
        total:0,
        host:app.d.hostUrl,
        remark:''
    },

    //初始化数据
    onLoad:function(options){
        var that = this;
        var userId = app.d.userId;
        that.setData({
            cartId: options.cartId,
            userId: userId,
        });
        that.loadProductDetail();
    },

    //加在商品信息
    loadProductDetail:function(){
        var that = this;
        wx.request({
            url: app.d.hostUrl + '/api/orders/cart-buy',
            method: 'get',
            header: {
                'Content-Type':  'application/x-www-form-urlencoded'
            },
            data:{
                cartId: that.data.cartId,
                userId: that.data.userId,
            },
            success:function(res){
                var status = res.data.status;
                if(status == 1){
                    that.setData({
                        total:res.data.info.count,
                        goodsInfo:res.data.info.cartInfo
                    });
                }else{
                    wx.showToast({
                        title: '网络错误！',
                        duration: 2000
                      });
                    return false;
                }
            },
        });
    },

    remarkInput:function(e){
        this.setData({
          remark: e.detail.value,
        })
    },

    //创建订单
    createProductOrderByWX:function(options){
        var that = this;

        wx.request({
            url: app.d.hostUrl + '/api/pay/cart-pay',
            method: 'get',
            header: {
                'Content-Type':  'application/x-www-form-urlencoded'
            },
            data:{
                cartId: that.data.cartId,
                userId: that.data.userId,
                total: that.data.total,
                remark: that.data.remark,
            },
            success: function(res){
                var status = res.data.status;
                if(status == 1){
                    that.wxpay(res.data.info);
                }else{
                  wx.showToast({
                    title:"下单失败!",
                    duration:2500
                  });
                }
            },
            fail: function (e) {
                wx.showToast({
                  title: '网络异常！err:createProductOrder',
                  duration: 2000
                });
            }
        });
    },

    //支付
    wxpay:function(res){
        var that = this;
        wx.request({
            url: app.d.hostUrl + '/api/pay/pay_now',
            method: 'get',
            header: {
                'Content-Type':  'application/x-www-form-urlencoded'
            },
            data:{
                orders_id: res.orders_id,
                orders_num: res.orders_num,
                userId: app.d.userId,
            },
            success:function(res){
                if(res.data.status==1){
                    var order=res.data.info;
                    wx.requestPayment({
                      timeStamp: order.timestamp,
                      nonceStr: order.nonceStr,
                      package: order.package,
                      signType: 'MD5',
                      paySign: order.paySign,
                      success: function(res){
                        wx.showToast({
                          title:"支付成功!",
                          duration:2000,
                        });
                        setTimeout(function(){
                          wx.navigateTo({
                            url: '../user/dingdan?currentTab=1&otype=deliver',
                          });
                        },2500);
                      },
                      fail: function(res) {
                        wx.showToast({
                          title:res,
                          duration:3000
                        })
                      }
                    })
                }else{
                    wx.showToast({
                      title: res.data.err,
                      duration: 2000
                    });
                }
            },
        });
    }
});